import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/yt_button.dart';
import 'package:yt_devops/pages/tasks/component/basic_info_item_widget.dart';
import 'package:yt_devops/pages/tasks/component/title_widget.dart';
import 'package:yt_devops/pages/tasks/component/wrap_widget.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/enums.dart';
import 'install_task_detail_controller.dart';

class InstallTaskDetailPage extends GetView<InstallTaskDetailController> {
  const InstallTaskDetailPage({Key? key}) : super(key: key);

  @override
  InstallTaskDetailController get controller =>
      Get.put(InstallTaskDetailController(), tag: Get.parameters['id']);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<InstallTaskDetailController>(
      init: controller,
      tag: Get.parameters['id'],
      builder: (controller) => Scaffold(
        appBar: AppBarWidget.withRightActions(
          ResKey.workOrderDetails.tr,
          actions: <Widget>[
            if (controller.enableEdit)
              GestureDetector(
                onTap: () => Get.toNamed(
                  RouterPath.createInstallTask,
                  arguments: {'id': controller.model.id},
                )?.then((value) {
                  if (value == true) {
                    controller.onInit();
                  }
                }),
                child: Padding(
                  padding: EdgeInsets.symmetric(
                    horizontal: 16.w,
                    vertical: 16.h,
                  ),
                  child: Image.asset(
                    ImageAssets.iconDeviceEdit,
                    fit: BoxFit.cover,
                  ),
                ),
              ),
          ],
        ),
        body: ListView(padding: EdgeInsets.zero, children: [
          WrapWidget(
            child: Column(children: [
              TitleWidget(
                ResKey.workOrderInfo.tr,
              ),
              ...controller.model.baseInfos
                  .map((e) => BasicInfoItemWidget(e))
                  .toList(),
              SizedBox(
                height: 8.h,
              ),
              //客户信息

              dividerWidget(),

              ...controller.model.installInfos
                  .map((e) => BasicInfoItemWidget(e))
                  .toList(),
              SizedBox(
                height: 8.h,
              ),
            ]),
          ),
          //任务进度
          WrapWidget(
            padding: EdgeInsets.fromLTRB(
              16.w,
              0,
              16.w,
              0,
            ),
            child:
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              TitleWidget(ResKey.schedule.tr),
              for (var i = 0; i < controller.model.tasilInfos.length; i++)
                BrnStepLine(
                  lineWidth: 1,
                  //非第一个是灰色
                  isGrey: controller.model.tasilInfos[i].id == null,
                  //最后一个的线条为透明色 做到不显示的效果
                  lineColor: i == controller.model.tasilInfos.length - 1
                      ? Colors.transparent
                      : controller.model.tasilInfos[i].id != null &&
                              controller.model.tasilInfos[i + 1].id != null
                          ? Colour.primaryColor
                          : const Color(0xffeeeeee),
                  contentWidget: Container(
                    padding: EdgeInsets.only(left: 10.w),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text('${controller.model.tasilInfos[i].name}',
                            style:
                                TextStyles.f14.boldStyle.copyWith(height: 0.9)),
                        Padding(
                          padding: const EdgeInsets.only(top: 12, bottom: 20),
                          child: Text(
                              controller.model.tasilInfos[i].processorName !=
                                      null
                                  ? '${controller.model.tasilInfos[i].processorName}  ${controller.model.tasilInfos[i].createTime}'
                                  : '',
                              style: TextStyles.lf14.copyWith(height: 0.9)),
                        ),
                      ],
                    ),
                  ),
                ),
            ]),
          ),

          //按钮
          if (controller.model.status ==
              InstallTaskStatusType.waitingForProcessing)
            Container(
                color: Colour.white,
                margin: EdgeInsets.only(top: 10.h),
                padding: EdgeInsets.fromLTRB(16.w, 16.h, 16.w, 16.h),
                child: SafeArea(
                    child: YtButton(
                  text: ResKey.receivingWorkOrders.tr,
                  onPress: () => controller.accept(
                      controller.model.id, controller.model.type!),
                )))
          else if (controller.model.status ==
                  InstallTaskStatusType.processing &&
              controller.model.siteId == null)
            Container(
                color: Colour.white,
                margin: EdgeInsets.only(top: 10.h),
                padding: EdgeInsets.fromLTRB(16.w, 16.h, 16.w, 16.h),
                child: SafeArea(
                  child: YtButton(
                      text: ResKey.createSite.tr,
                      onPress: () => controller.toCreateSite()),
                ))
          else if (controller.model.status ==
                  InstallTaskStatusType.processing &&
              controller.model.siteId != null)
            Container(
                color: Colour.white,
                margin: EdgeInsets.only(top: 10.h),
                padding: EdgeInsets.fromLTRB(16.w, 16.h, 16.w, 16.h),
                child: SafeArea(
                  child: YtButton(
                      text: ResKey.completeWorkOrders.tr,
                      onPress: () => controller.toFinish(
                            controller.model.id,
                          )),
                )) // 完成订单
          else
            Container(
              color: Colour.white,
              padding: EdgeInsets.fromLTRB(16.w, 0, 16.w, 37.h),
            )
        ]),
      ),
    );
  }

  Widget dividerWidget() {
    return const Divider(
      color: Color(0xFFEEEEEE),
    );
  }
}
